package com.example.compose

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.EaseInOut
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Preview
@Composable
fun FillAnimation() {
    var isCarFlipped by remember { mutableStateOf(false) }
    val animDuration = 900
    val zAxisDistance = 10f
    val frontColor by animateColorAsState(
        label = "",
        targetValue = if (isCarFlipped) Color(0xff789fff) else Color(0xff282a31),
        animationSpec = tween(durationMillis = animDuration, easing = EaseInOut)
    )
    val rotateCarY by animateFloatAsState(
        label = "",
        targetValue = if (isCarFlipped) 180f else 0f,
        animationSpec = tween(durationMillis = animDuration, easing = EaseInOut)
    )

    val textAlpha by animateFloatAsState(
        label = "",
        targetValue = if (isCarFlipped) 0f else 1f,
        animationSpec = tween(durationMillis = 1500)
    )

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Box(
            modifier = Modifier
                .size(width = 200.dp, height = 300.dp)
                .graphicsLayer {
                    rotationY = rotateCarY
                    cameraDistance = zAxisDistance
                }
                .clip(shape = RoundedCornerShape(24.dp))
                .clickable { isCarFlipped = !isCarFlipped }
                .background(color = frontColor)
        ) {
            if(rotateCarY > 90f) {
                Image(
                    painter = painterResource(id = R.drawable.koda),
                    contentDescription = "",
                    contentScale = ContentScale.Crop
                )
            } else {
                Image(
                    painter = painterResource(id = R.drawable.nox),
                    contentDescription = "",
                    contentScale = ContentScale.Crop
                )
            }

        }
        Spacer(modifier = Modifier.height(32.dp))
        Text(
            modifier = Modifier.graphicsLayer {
                alpha = if (isCarFlipped) 1f - textAlpha else textAlpha
            },
            text = if (isCarFlipped) "Reveal" else "hide",
            color = Color.Black
        )
    }
}